package com.example.recruitment.controller;

import com.example.recruitment.entity.Resume;
import com.example.recruitment.entity.User;
import com.example.recruitment.entity.Job;
import com.example.recruitment.service.ResumeService;
import com.example.recruitment.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private JobService jobService;

    @GetMapping("/list")
    public String list(Model model) {
        List<Resume> resumes = resumeService.findAll();
        model.addAttribute("resumes", resumes);
        return "resume/list";
    }

    @GetMapping("/my")
    public String myResumes(HttpSession session, Model model) {
        Long userId = ((User) session.getAttribute("user")).getId();
        List<Resume> resumes = resumeService.findByUserId(userId);
        model.addAttribute("resumes", resumes);
        return "resume/my";
    }

    @GetMapping("/job/{jobId}")
    public String jobResumes(@PathVariable Long jobId, Model model) {
        List<Resume> resumes = resumeService.findByJobId(jobId);
        model.addAttribute("resumes", resumes);
        return "resume/list";
    }

    @GetMapping("/add")
    public String addPage(Model model) {
        List<Job> jobs = jobService.findAll();
        model.addAttribute("jobs", jobs);
        return "resume/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Resume resume, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 从session中获取用户ID
            User user = (User) session.getAttribute("user");
            if (user == null) {
                result.put("success", false);
                result.put("message", "请先登录");
                return result;
            }
            
            // 设置用户ID
            resume.setUserId(user.getId());
            
            // 设置默认状态为"待审核"
            resume.setStatus("待审核");
            
            // 保存简历
            boolean success = resumeService.add(resume);
            result.put("success", success);
            result.put("message", success ? "简历创建成功" : "简历创建失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "简历创建失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Long id, Model model) {
        Resume resume = resumeService.findById(id);
        List<Job> jobs = jobService.findAll();
        model.addAttribute("resume", resume);
        model.addAttribute("jobs", jobs);
        return "resume/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    public String update(@RequestBody Resume resume) {
        // 获取原有简历信息
        Resume oldResume = resumeService.findById(resume.getId());
        // 保留原有的user_id
        resume.setUserId(oldResume.getUserId());
        
        if (resumeService.update(resume)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Long id) {
        if (resumeService.deleteById(id)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/status/{id}")
    @ResponseBody
    public String updateStatus(@PathVariable Long id, @RequestParam String status) {
        if (resumeService.updateStatus(id, status)) {
            return "success";
        }
        return "fail";
    }
} 